﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GameForm.Gamelib
{
    public class GameApi
    {
        protected Dm.dmsoft dm = null;
        public GameApi(Dm.dmsoft dm)
        {
            this.dm = dm;
        }

        public bool CheckColor(int x, int y, string color)
        {
            var tc = dm.GetColor(x, y);
            return tc == color;
        }

        public void LeftClick(int x, int y)
        {
            dm.MoveTo(x, y);
            dm.LeftClick();
        }

        public async Task<bool> Dlg_快捷任务_选中()
        {
            while (dm.GetColor(869, 124) == "a59b73") //当前没有选中任务栏
            {
                dm.MoveTo(869, 124);
                dm.LeftClick();
                await Task.Delay(1000);
            }
            return dm.GetColor(869, 124) == "eddda5";
        }

        public async Task<bool> Dlg_快捷队伍_选中()
        {
            if (CheckColor(1003, 123, "a9a07a"))
            {
                LeftClick(1003, 123);
                await Task.Delay(1000);
            }
            if (CheckColor(1003, 123, "f3e5af"))
                return true;
            return false;
        }

        public async Task<bool> Dlg_任务_打开()
        {
            if (dm.GetColor(869, 124) == "a59b73") //当前没有选中任务栏
            {
                dm.MoveTo(869, 124);
                dm.LeftClick();
                await Task.Delay(1000);
            }

            if (dm.GetColor(869, 124) == "eddda5") //当前选中任务栏
            {
                dm.MoveTo(869, 124);
                dm.LeftClick();
                await Task.Delay(1000);
            }

            if (dm.GetColor(474, 96) == "764a2c" && dm.GetColor(528, 107) == "764a2c" && dm.GetColor(925, 107) == "cf0000")
            {
                //任务栏已经打开
                return true;
            }
            return false;
        }

        /// <summary>
        /// 摆摊窗口被打开
        /// </summary>
        /// <returns></returns>
        public bool Dlg_摆摊_IsOpen()
        {
            return dm.GetColor(488, 114) == "fefebb" && dm.GetColor(526, 114) == "fefebb" && dm.GetColor(932, 104) == "cf0000";
        }

        /// <summary>
        /// 是否在主界面
        /// </summary>
        /// <returns></returns>
        public bool Dlg_主界面_IsOpen()
        {
            return dm.GetColor(1, 28) == "ecddcb";
        }


        /// <summary>
        /// 对话框打开
        /// </summary>
        /// <returns></returns>
        public bool Dlg_Npc对话_IsOpen()
        {
            return dm.GetColor(22, 667) == "e9e29a" && dm.GetColor(1002, 667) == "e9e29a";
        }


        /// <summary>
        /// 上交物品窗口
        /// </summary>
        /// <returns></returns>
        public bool Dlg_上交物品_IsOpen()
        {
            return dm.GetColor(771, 157) == "553923" && dm.GetColor(859, 151) == "553923" && dm.GetColor(995, 159) == "c01400";
        }

        /// <summary>
        ///  战斗中
        /// </summary>
        /// <returns></returns>
        public bool Dlg_战斗中_IsOpen()
        {
            return dm.CmpColor(43, 47, "f7c67e", 0.9) == 0 || dm.CmpColor(371, 48, "ffe699", 0.9) == 0;
        }

        public bool Dlg_Npc商铺_IsOpen()
        {
            return dm.GetColor(494, 87) == "764a2c" && dm.GetColor(535, 94) == "764a2c" && dm.GetColor(959, 108) == "cf0000";
        }


        public bool Dlg_商会_IsOpen()
        {
            return dm.GetColor(495, 116) == "fefebd" && dm.GetColor(530, 116) == "fefdba" && dm.GetColor(932, 104) == "cf0000";
        }

        public bool Dlg_抓鬼_缺人提示()
        {
            return dm.GetColor(294, 325) == "ddac6f" && dm.GetColor(379, 441) == "48301e" && dm.GetColor(599, 449) == "48301e";
        }

        public async Task<bool> Action_上交物品()
        {
            if (Dlg_上交物品_IsOpen())
            {
                dm.MoveTo(812, 600);
                dm.LeftClick();
                return true;
            }
            await Task.Delay(0);
            return false;
        }

        public async Task<bool> Action_关闭杂框()
        {
            object x, y;

            var nRet = dm.FindMultiColor(504, 82, 1015, 460, "fff7d9", "6|0|fff8de,-1|4|fff4d3,6|4|fff7d8", 1, 0, out x, out y);
            if (nRet == 1)
            {
                LeftClick((int)x, (int)y);
                return true;
            }
            if(CheckColor(915, 170, "bf1500"))
            {
                LeftClick(915, 170);
                return true;
            }

            await Task.Delay(0);
            return false;
        }

        public async Task<bool> Action_关闭无干扰框()
        {
            if (dm.GetColor(730, 514) == "a06030")
            {
                LeftClick(730, 514);
                return true;
            }
            await Task.Delay(0);
            return false;
        }

        public async Task<bool> Action_购买摆摊任务物品()
        {
            if (Dlg_摆摊_IsOpen())
            {
                dm.MoveTo(472, 246); //点击第一个商品
                dm.LeftClick();
                await Task.Delay(1000);
                dm.MoveTo(868, 648); //点击购买商品
                dm.LeftClick();
                return true;
            }
            return false;
        }

        public async Task<bool> Action_购买Npc物品()
        {
            if (Dlg_Npc商铺_IsOpen())
            {
                dm.MoveTo(804, 583);
                dm.LeftClick();
                return true;
            }
            await Task.Delay(0);
            return false;
        }

        public async Task<bool> Action_购买商会物品()
        {
            if (Dlg_商会_IsOpen())
            {
                dm.MoveTo(849, 647);
                dm.LeftClick();
                return true;
            }
            await Task.Delay(0);
            return false;
        }

        public async Task<bool> Action_Npc对话()
        {
            if (Dlg_Npc对话_IsOpen())
            {
                object x, y;
                var nRet = dm.FindMultiColor(666, 115, 868, 584, "fdfadd", "22|0|fefce1", 1, 0, out x, out y);
                if (nRet == 1)
                {
                    dm.MoveTo((int)x, (int)y);
                    dm.LeftClick();
                    await Task.Delay(1000);
                    return await Action_Npc对话();
                }
                else
                {
                    //空对话 随便点
                    dm.MoveTo(22, 667);
                    dm.LeftClick();
                }
                return true;
            }
            return false;
        }


        public async Task<bool> Action_点击任务()
        {
            if (Dlg_主界面_IsOpen())
            {
                object x, y;
                var nRet = dm.FindColor(803, 156, 1021, 325, "fcf31c-202020", 1, 0, out x, out y);
                if (nRet == 1)
                {
                    dm.MoveTo((int)x, (int)y);
                    dm.LeftClick();
                    return true;
                }
            }
            await Task.Delay(0);
            return false;
        }

        public async Task<bool> Action_使用物品()
        {
            if (dm.GetColor(917, 479) == "c01400" && dm.GetColor(842, 633) == "6c310a")
            {
                dm.MoveTo(842, 633);
                dm.LeftClick();
                await Task.Delay(5000);
                return true;
            }
            return false;
        }


        public async Task<bool> Action_继续抓鬼()
        {
            if (dm.GetColor(297, 420) == "ddac6f" && dm.GetColor(373, 432) == "48301e" && dm.GetColor(599, 436) == "48301e")
            {
                //继续抓鬼对话框
                dm.MoveTo(599, 436);
                dm.LeftClick();
                return true;
            }
            await Task.Delay(0);
            return false;
        }

        public async Task<bool> Action_抓鬼_缺人_自动匹配()
        {
            if (Dlg_抓鬼_缺人提示())
            {
                dm.MoveTo(599, 449);
                dm.LeftClick();
                return true;
            }
            await Task.Delay(0);
            return false;
        }

        public async Task<bool> Action_抓鬼_没有辅助门派()
        {
            if(CheckColor(403, 460, "48301e") && CheckColor(594, 461, "49311f"))
            {
                LeftClick(403, 460);
                await Task.Delay(500);
                return true;
            }

            if(CheckColor(403, 446, "48301e")  && CheckColor(601, 453, "4a321f"))
            {
                LeftClick(403, 446);
                await Task.Delay(500);
                return true;
            }
            return false;
        }


        public async Task<bool> Action_踢队友(int nIndex)
        {
            LeftClick(157 + nIndex * 171, 364);
            await Task.Delay(500);
            if (nIndex < 3)
            {
                if (CheckColor(431 + nIndex * 171, 377, "553923"))
                {
                    LeftClick(431 + nIndex * 171, 377);
                    return true;
                }
            }
            else
            {
                if (CheckColor(-20 + nIndex * 171, 380, "553923"))
                {
                    LeftClick(-20 + nIndex * 171, 380);
                    return true;
                }
            }

            return false;
        }

        public async Task<bool> Action_踢掉离线队友()
        {
            var numCount = Get_队友人数();
            //if (numCount == 5)
            //    return false;
            for (var i = 1; i < numCount; i++)
            {
                if (Ck_队友离线(i))
                {
                    if (await Action_踢队友(i))
                    {
                        await Task.Delay(1000);
                        return await Action_踢掉离线队友() || true;
                    }
                }
            }
            return numCount < 5;
        }

        public async Task<bool> Dlg_队伍_IsOpen()
        {
            await Task.Delay(0);
            return CheckColor(524, 95, "764a2c") && CheckColor(925, 103, "cf0000");
        }

        public async Task<bool> Dlg_队伍_打开(bool isKilling)
        {
            if (isKilling)
            {
                if (dm.GetColor(43, 47) == "f7c67e")
                {
                    LeftClick(43, 47);
                    await Task.Delay(1000);
                }
                if (dm.GetColor(371, 48) == "ffe699" && CheckColor(108, 225, "f6e7ad"))
                {
                    LeftClick(108, 225);
                    await Task.Delay(1000);
                }
            }
            else
            {
                if (CheckColor(1003, 123, "a9a07a"))
                {
                    LeftClick(1003, 123);
                    await Task.Delay(1000);
                }
                if (CheckColor(1003, 123, "f3e5af"))
                {
                    LeftClick(1003, 123);
                    await Task.Delay(1000);
                }
            }
            return await Dlg_队伍_IsOpen();
        }

        public async Task<bool> Action_匹配队友()
        {
            if (CheckColor(701, 167, "a4622f")) //匹配目标-编辑
            {
                LeftClick(701, 167);
                await Task.Delay(1000);

                object x, y;
                var nRet = dm.FindMultiColor(272, 184, 499, 600, "664a33", "8|13|664a32,26|12|553923,60|11|5e422c", 1, 0, out x, out y);
                if (nRet == 1)
                {
                    LeftClick((int)x, (int)y);
                    await Task.Delay(500);
                    for (var i = 0; i < 3; i++)
                    {
                        dm.MoveTo(663, 280);
                        dm.LeftDown();
                        await Task.Delay(500);
                        dm.MoveTo(661, 202);
                        dm.LeftUp();
                        await Task.Delay(500);
                    }
                }

                while (CheckColor(665, 653, "6f340c"))
                {
                    LeftClick(665, 653);
                    await Task.Delay(1000);
                }
                return true;
            }
            return false;
        }
        public async Task<bool> Action_工坊摆摊购买物品()
        {
            if (CheckColor(444, 85, "eed469") && CheckColor(557, 108, "f6e298") && CheckColor(978, 187, "fef582"))
            {
                LeftClick(450, 269);
                await Task.Delay(1000);
                LeftClick(827, 639);
                await Task.Delay(1000);
                return true;
            }
            return false;
        }

        public async Task<bool> Dlg_活动_IsOpen()
        {
            await Task.Delay(0);
            return CheckColor(490, 101, "8d5d2c") && CheckColor(958, 108, "cf0000");
        }
        public async Task<bool> Dlg_活动_打开()
        {
            if (CheckColor(306, 52, "f7ecba"))
            {
                LeftClick(306, 52);
                await Task.Delay(1000);
            }
            return await Dlg_活动_IsOpen();
        }

        public async Task<bool> Action_点击活动任务(string taskName)
        {
            if (!await Dlg_活动_打开())
                return false;
            object x, y;
            for (var i = 0; i < 6; i++)
            {
                var nRet = dm.FindPic(235, 145, 684, 506, $"活动_{taskName}_图标.bmp", "202020", 0.8, 0, out x, out y);
                if (nRet >= 0)
                {
                    int nx = (int)x, ny = (int)y;

                    nRet = dm.FindPic(nx + 184, ny - 35, nx + 322, ny + 35, "活动_参加_图标.bmp", "202020", 0.8, 0, out x, out y);
                    if (nRet >=0)
                    {
                        dm.MoveTo((int)x, (int)y);
                        dm.LeftClick();
                        await Task.Delay(1000);
                        return true;
                    }
                }
                if (i < 3)
                {
                    #region 向上拉动任务
                    dm.MoveTo(516, 491);
                    dm.LeftDown();
                    await Task.Delay(100);
                    dm.MoveTo(470, 147);
                    dm.LeftUp();
                    #endregion
                }
                else
                {
                    #region 向下拉动任务                    
                    dm.MoveTo(470, 147);
                    dm.LeftDown();
                    await Task.Delay(100);
                    dm.MoveTo(516, 491);
                    dm.LeftUp();
                    #endregion
                }
                await Task.Delay(2000);
            }
            //关闭活动窗口
            while (await Dlg_活动_IsOpen())
            {
                LeftClick(958, 108);
                await Task.Delay(1000);
            }
            return false;
        }

        public async Task<bool> Action_队友状态检测(bool isKilling)
        {
            if (!await Dlg_队伍_打开(isKilling))
                return false;
            if (CheckColor(957, 187, "b18051"))
            {
                LeftClick(957, 187);
                await Task.Delay(1000);
            }
            if (await Action_踢掉离线队友())
            {
                await Action_匹配队友();
                await Task.Delay(1000);
            }
            //关闭队伍
            while (await Dlg_队伍_IsOpen())
            {
                LeftClick(925, 102);
                await Task.Delay(1000);
            }
            return true;
        }

        public async Task<bool> Ck_移动中()
        {
            var c1 = dm.GetColorNum(117, 52, 201, 69, "ede0cf-7070a0", 1);
            await Task.Delay(1000);
            var c2 = dm.GetColorNum(117, 52, 201, 69, "ede0cf-7070a0", 1);
            return c1 != c2;
        }


        public async Task<bool> Wait_战斗中()
        {
            while (Dlg_战斗中_IsOpen())
                await Task.Delay(1000);
            return false;
        }

        public bool Ck_队友离线(int nIndex)
        {
            return dm.GetColor(145 + (nIndex * 171), 469) == "e9f9f8" || dm.GetColor(166 + (nIndex * 171), 469) == "e9f9f8";
        }

        public int Get_队友人数()
        {
            for (var i = 0; i < 5; i++)
            {
                if (dm.GetColor(147 + (i * 171), 614) != "d9ac77")
                {
                    if (!Ck_队友离线(i))
                        return i;
                }
            }
            return 5;
        }


    }
}
